package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.Credential;
import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.entity.Banco;
import edu.upc.clase.demo.service.BancoService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author gian
 */
@Controller("bancoController")
public class BancoController {

    @Autowired
    private BancoService bancoService;
    private static Logger log = LoggerFactory.getLogger(BancoController.class);

    @RequestMapping("/banco/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("banco/index");
        List<Banco> banco = bancoService.buscarTodos();
        CriterioBusqueda criterioBusqueda = new CriterioBusqueda();
        mav.addObject("banco", banco);
        mav.addObject("criterioBusqueda",criterioBusqueda);
        return mav;
    }
    
    @RequestMapping(value = "/banco/new", method = RequestMethod.GET)
    public ModelAndView newBanco() {
        ModelAndView mav = new ModelAndView("banco/new");
        Banco banco = new Banco();
        mav.getModelMap().put("banco", banco);
        return mav;
    }

    @RequestMapping(value = "/banco/new", method = RequestMethod.POST)
    public String createBanco(@ModelAttribute("banco")Banco banco, SessionStatus status) {    
        bancoService.insertar(banco);
        status.setComplete();

        return "redirect:/pages/banco/index";
        
    }
    
    @RequestMapping(value = "/banco/edit", method = RequestMethod.GET)
    public ModelAndView editBanco(@RequestParam("id")Integer id) {    
        ModelAndView mav = new ModelAndView("banco/edit");
        Banco banco = bancoService.buscar(id);
        mav.getModelMap().put("banco", banco);
        return mav;        
    }
    
    @RequestMapping(value="/banco/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("banco") Banco banco, SessionStatus status) {
        bancoService.actualizar(banco);
        status.setComplete();
        return "redirect:/pages/banco/index";
    }
    
    @RequestMapping("/banco/delete")
    public ModelAndView delete(@RequestParam("id")Integer id)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/banco/index");
        Banco banco = bancoService.buscar(id);
        bancoService.eliminar(banco);
        return mav;
    }
    
   
    @RequestMapping(value = "/banco/buscar", method=RequestMethod.POST)
    public ModelAndView buscar(@ModelAttribute("criterioBusqueda") CriterioBusqueda criterio, SessionStatus status) {
        ModelAndView mav = new ModelAndView("banco/resultadoBusqueda");
        log.info("resultado = " + bancoService.buscarPorNombre(criterio.getNombre()).size());
        mav.getModel().put("resultado",bancoService.buscarPorNombre(criterio.getNombre()));
        return mav;
    }
    
}
